<?php
/**
 * Created by PhpStorm.
 * User: Sergey
 * Date: 26.03.2014
 * Time: 11:38
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

define("BASE_DQ_PLUGIN_PATH", dirname(__FILE__) . "/");

require(realpath(dirname(__FILE__) . "../../../../") . '/wp-blog-header.php');
require_once BASE_DQ_PLUGIN_PATH . "classes/QuizDao.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/QuestionDao.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/UserDao.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/GroupDao.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/ResultDao.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/QuizResultChecker.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/MarkConverter.php";

$quizDao = new QuizDao();
$questionDao = new QuestionDao();
$userDao = new UserDao();
$groupDao = new GroupDao();
$resultDao = new ResultDao();
$checker = new QuizResultChecker();

if (!isset($_REQUEST["id"])
    || intval($_REQUEST["id"]) <= 0
//    || !$quizDao->getById(intval($_REQUEST["id"]))
) {
    header("Location: /");
}

// TODO: check tutor permissions

if (isset($_REQUEST["id"]) && intval($_REQUEST["id"]) > 0
    && isset($_REQUEST["action"]) && $_REQUEST["action"] == "removeResults"
    && isset($_REQUEST["studentId"]) && intval($_REQUEST["studentId"]) > 0
) {
    $resultDao->remove($_REQUEST["id"], $_REQUEST["studentId"]);
    header(
        "Location: " . menu_page_url($_GET["page"], false) .
        "&id=" . $_REQUEST["id"]
    );
}

$quiz = $quizDao->getById(intval($_GET["id"]));

// Fix for page title
$wp_query->is_404 = false;
?>

<? include_once BASE_DQ_PLUGIN_PATH . "header.php"; ?>

<div id="content" class="left">
    <h3>Результаты теста</h3>

    <h2 class="entry-title">
        <b>"<?= $quiz->name ?>"</b>
    </h2>
    <hr>
    <br>

    <? foreach ($quiz->groups as $groupId) { ?>
        <? $students = $userDao->getAllStudentsBy($groupId); ?>
        <h2><?= $groupDao->getById($groupId)->name ?></h2>

        <table class="content-table">
            <thead>
            <tr>
                <th rowspan="2">ФИО студента</th>
                <th colspan="4">Результаты теста</th>
                <th rowspan="2">Операции</th>
            </tr>
            <tr>
                <th>Ответы</th>
                <th>Система 1 - 5</th>
                <th>Система 0 - 100</th>
                <th>Система ABCDEF</th>
            </tr>
            </thead>
            <tbody>
            <? foreach ($students as $student) { ?>
                <? $quizResult = $checker->check($quiz->id, $student->id); ?>
                <tr>
                    <td><?= $student->displayName ?></td>
                    <? if ($quizResult) { ?>
                        <td>
                            <b><?=
                                $quizResult->score . " из " .
                                $quizResult->maxScore
                                ?></b>
                            <br>
                        </td>
                        <td><?=
                            MarkConverter::to_5_point(
                                $quizResult->score,
                                $quizResult->maxScore
                            ) ?></td>
                        <td><?=
                            MarkConverter::to_100_point(
                                $quizResult->score,
                                $quizResult->maxScore
                            ) ?></td>
                        <td><?=
                            MarkConverter::to_ABCDEF_point(
                                $quizResult->score,
                                $quizResult->maxScore
                            ) ?></td>
                    <? } else { ?>
                        <td>---</td>
                        <td>---</td>
                        <td>---</td>
                        <td>---</td>
                    <? } ?>
                    <td>
                        <a href="<?=
                        menu_page_url("distant-quiz/quiz-results.php", false) .
                        "&id=" . $quiz->id .
                        "&action=removeResults" .
                        "&studentId=" . $student->id
                        ?>"
                           class="jquery-button"
                           title="Очистить результаты для данного студента">Очистить</a>
                    </td>
                </tr>
            <? } ?>
            </tbody>
        </table>
        <br>
    <? } ?>
</div>